package com.hawthorn.admin.repository.provider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.component.constant.DicConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;

import java.util.Map;

/**
 * <p>todo 消息管理</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/8/11 12:00
 * @version v1.0.2
 * @copyright: Copyright (c)  andyten
 */
@Slf4j
public class SysMessageSqlProvider implements ProviderMethodResolver
{
  public String findAllMsg(Page<Map<String, Object>> page, String conditions, String userAccount, Long deptId, Long oemCompanyId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append("   a.id, \n ");
    sql.append("   a.create_by createBy, \n ");
    sql.append("   o.sname, \n ");
    sql.append("   a.url, \n ");
    sql.append("   a.type, \n ");
    sql.append("   a.create_time createTime, \n ");
    sql.append("   a.title, \n ");
    sql.append("   a.dept_id deptId, \n ");
    sql.append("   a.content, \n ");
    sql.append("   a.biz_pk bizPk\n ");
    sql.append("     from sys_message a \n ");
    sql.append("     left join sys_dept o \n ");
    sql.append("     on a.dept_id = o.id \n ");
    sql.append("     where 1 = 1 \n ");
    // sql.append("     a.status = " + DicConstant.TXXXZT_01 + "\n ");
    // sql.append("     and a.type = " + DicConstant.TXLX_01 + " \n");
    sql.append("     and a.oem_company_id = " + oemCompanyId + "\n ");
    sql.append("     and now() >= a.create_time + a.overrun / 24 \n ");
    sql.append("     and (a.accept_account = '" + userAccount + "' or a.accept_dept = " + deptId + ")\n ");
    return sql.toString();
  }

  public String findMsgOneWeek(String userAccount, Long oemCompanyId, Long deptId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append("   a.id, \n ");
    sql.append("   a.create_by createBy, \n ");
    sql.append("   o.sname, \n ");
    sql.append("   a.url, \n ");
    sql.append("   a.type, \n ");
    sql.append("   date_format(a.create_time, '%Y-%m-%d %H:%i') createTime, \n ");
    sql.append("   a.title, \n ");
    sql.append("   a.content, \n ");
    sql.append("   a.biz_pk bizPk\n ");
    sql.append("     from sys_message a \n ");
    sql.append("     left join sys_dept o \n ");
    sql.append("     on a.dept_id = o.id \n ");
    sql.append("     where  \n ");
    sql.append("     a.status = " + DicConstant.TXXXZT_01 + "\n ");
    sql.append("     and a.type = " + DicConstant.TXLX_01 + " \n");
    sql.append("     and a.oem_company_id = " + oemCompanyId + "\n ");
    sql.append("     and now() >= a.create_time + a.overrun / 24 \n ");
    sql.append("     and (a.accept_account = '" + userAccount + "' or a.accept_dept = " + deptId + ")\n ");
    sql.append("     and a.create_time > date_sub(now(), interval 1 week) \n ");
    sql.append("     order by a.type,a.create_time  \n ");
    return sql.toString();
  }

  public String findMsgOneWeek2(String userAccount, Long oemCompanyId, Long deptId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append("   a.id, \n ");
    sql.append("   a.create_by createBy, \n ");
    sql.append("   o.sname, \n ");
    sql.append("   a.url, \n ");
    sql.append("   a.type, \n ");
    sql.append("   date_format(a.create_time, '%Y-%m-%d %H:%i') createTime, \n ");
    sql.append("   a.title, \n ");
    sql.append("   a.content, \n ");
    sql.append("   a.biz_pk bizPk\n ");
    sql.append("     from sys_message a \n ");
    sql.append("     left join sys_dept o \n ");
    sql.append("     on a.dept_id = o.id \n ");
    sql.append("     where  \n ");
    sql.append("     a.status = " + DicConstant.TXXXZT_01 + "\n ");
    sql.append("     and a.type in (" + DicConstant.TXLX_03 + "," + DicConstant.TXLX_02 + ") \n");
    sql.append("     and a.oem_company_id = " + oemCompanyId + "\n ");
    sql.append("     and now() >= a.create_time + a.overrun / 24 \n ");
    sql.append("     and (a.accept_account = '" + userAccount + "' or a.accept_dept = " + deptId + ")\n ");
    sql.append("     and a.create_time > date_sub(now(), interval 1 week) \n ");
    sql.append("     order by a.type,a.create_time  \n ");
    return sql.toString();
  }

  public String findMsgOneWeekNums(String userAccount, Long oemCompanyId, Long deptId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append("     count(1) amount \n ");
    sql.append("     from sys_message a \n ");
    sql.append("     left join sys_dept o \n ");
    sql.append("     on a.dept_id = o.id \n ");
    sql.append("     where  \n ");
    sql.append("     a.status = " + DicConstant.TXXXZT_01 + "\n ");
    sql.append("     and a.oem_company_id = " + oemCompanyId + "\n ");
    sql.append("     and now() >= a.create_time + a.overrun / 24 \n ");
    sql.append("     and (a.accept_account = '" + userAccount + "' or a.accept_dept = " + deptId + ")\n ");
    sql.append("     and a.create_time > date_sub(now(), interval 1 week) \n ");
    // sql.append("     group by a.type \n ");
    return sql.toString();
  }
}
